﻿@model IEnumerable<WebApp.Models.UserViewModel>

@{
    ViewBag.Title = "User Accounts";
    Layout = "~/Views/Shared/_Layout.cshtml";
    int usr_index_serial = 1;
}

<div class="box-body table-responsive no-padding">
    <table class="table table-bordered table-hover table-striped">
        <tr>
            <th style="width:50px;" valign="middle">
                <div class="btn-group-xs">
                    <a title="Register a new user" href="@Url.Action("Register","Account")" class="btn btn-info"><i class="fa fa-plus"></i>&nbsp;New</a>
                </div>
            </th>
            <th>Email</th>
            <th>Role</th>
            <th style="min-width:120px;">&nbsp;</th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td align="right">@(usr_index_serial++).</td>
                <td>
                    @Html.DisplayFor(modelItem => item.Email)
                </td>
                <td>
                    @{
                        string roleString = "&nbsp;";
                        if (!string.IsNullOrWhiteSpace(item.Role))
                        {
                            roleString = item.Role;
                        }
                    }
                    <span>@roleString</span>
                </td>
                <td class="text-center to-mid" align="center" style="min-width:130px;">
                    <div>
                    @if (User.Identity.Name != item.Email)
                    {
                        using (Html.BeginForm("ChangeRole", "User", FormMethod.Post, new { @style = "width:100px;display:inline-block;" }))
                        {
                            <input type="hidden" value="@item.Id" name="UserId" id="UserId" />
                            @Html.AntiForgeryToken();
                            <div class="btn-group-xs">
                                @if (item.IsAdmin)
                                {
                                    <input type="hidden" value="false" name="IsMakeAdmin" id="IsMakeAdmin" />
                                    <input type="submit" value="Remove Admin" class="btn btn-danger user-admin-button" />
                                }
                                else
                                {
                                    <input type="hidden" value="true" name="IsMakeAdmin" id="IsMakeAdmin" />
                                    <input type="submit" value="Make Admin" class="btn btn-success user-admin-button" />
                                }
                            </div>
                        }
                        using (Html.BeginForm("Delete", "User", FormMethod.Post, new { @style = "width:30px;display:inline-block;" }))
                        {
                            <input type="hidden" value="@item.Id" name="UserId" id="UserId" />
                            @Html.AntiForgeryToken();
                            <div class="btn-group-xs">
                                <button type="submit" class="btn btn-danger">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </div>
                        }
                    }
                    else
                    {
                        <div class="btn-group-xs" style="display:inline-block;">
                            @if (item.IsAdmin)
                            {
                                <button disabled class="btn btn-danger user-admin-button disabled">
                                    Remove Admin
                                </button>
                            }
                            else
                            {
                                <button disabled class="btn btn-success user-admin-button disabled">
                                    Make Admin
                                </button>
                            }
                        </div>
                        <div class="btn-group-xs" style="display:inline-block;">
                            <button disabled class="btn btn-danger disabled">
                                <i class="fa fa-trash"></i>
                            </button>
                        </div>
                    }
                </div>
            </td>
        </tr>
        }
    </table>
</div>